package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;

    @Override
    @Transactional //因为涉及到多表 要开启事务信息
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息
        this.save(dishDto);
        // 设置菜品与口味的关系
        //获取口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //将获取的菜品id 存入到口味信息内
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品信息 在表dish中查询
        Dish dish = this.getById(id);
        //构建DishDto
        DishDto dishDto =new DishDto();
        //拷贝
        BeanUtils.copyProperties(dish,dishDto);
        //创建条件构造器
        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        //查询dish对象中的id 赋值给DishFlavor中的dish_id
        queryWrapper.eq("dish_id",dish.getId());
        //在dishFlavorService根据构造器获取的id查询对应的口味集合
        List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);
        //将获取的口味集合封装到dishDto中
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    //修改菜品信息
    @Override
    @Transactional//因为涉及到多表 要开启事务信息保证一致性
    public void updateWithFlavor(DishDto dishDto) {
        //修改基本信息
        this.updateById(dishDto);
        //创建条件构造器  清理菜品口味数据 dish_flavor表的delete操作
        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id",dishDto.getId());
        //删除口味信息
        dishFlavorService.remove(queryWrapper);
        //添加新的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //将口味信息id存入flavors中
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }
    //删除信息
    @Override
    public void deleteWithFlavor(Long[] ids) {
            List<Long> longs = Arrays.asList(ids);
              this.removeByIds(longs);
            //创建条件构造器  清理菜品口味数据 dish_flavor表的delete操作
            QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("dish_id",longs);
            //删除口味信息
            dishFlavorService.remove(queryWrapper);

    }

    //修改状态信息
    @Override
    public void updateStatus(Integer status, Long [] ids) {
            for (Long id : ids) {
            Dish dish =new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }
    }
}
